package cn.layfolk.nio.zerocopy;

import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

/**
 * @Author 王津
 * @Date 2020/9/29
 * @desc
 */
public class NewIOClient {
    public static void main(String[] args) throws  Exception {

        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(new InetSocketAddress("localhost",7001));
        String filename = "Koala.jpg";

        //得到一个文件 channel
        FileChannel fileChannel  = new FileInputStream(filename).getChannel();
        //准备发送
        long startTime = System.currentTimeMillis();

        //在linux下一个transferTo 方法就可以完成传输
        //在windows下一次调用transferTo 只能发送8m，就需要分段传输文件，而且要主要
        //传输时的位置。。思考
        //tansferTo底层使用到零拷贝
        long transferCount = fileChannel.transferTo(0, fileChannel.size(), socketChannel);
        System.out.println("发送的总的字节数 ="+transferCount+"耗时："+(System.currentTimeMillis()-startTime));

        //关闭
        fileChannel.close();
    }
}
